package com.fileupload.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 配置文件读取器
 * @author 小乙
 */
@Component
public class ConfigReader {
    private static final Logger logger = LoggerFactory.getLogger(ConfigReader.class);

    @Autowired
    private ApplicationConfig applicationConfig;

    private Map<String, ServerConfig> serverConfigMap = new HashMap<>();
    private Map<String, MonitorConfig> monitorConfigMap = new HashMap<>();

    @PostConstruct
    public void init() {
        logger.info("开始初始化配置文件读取器");
        
        // 检查applicationConfig是否为null
        if (applicationConfig == null) {
            logger.error("applicationConfig为null，无法加载配置");
            return;
        }
        
        // 调试信息：打印applicationConfig对象状态
        logger.info("applicationConfig对象: {}", applicationConfig);
        logger.info("服务器配置列表: {}", applicationConfig.getServers());
        logger.info("监控配置列表: {}", applicationConfig.getMonitorGroup());
        
        // 初始化服务器配置映射
        if (applicationConfig.getServers() != null) {
            logger.info("开始加载服务器配置，数量: {}", applicationConfig.getServers().size());
            for (ServerConfig server : applicationConfig.getServers()) {
                serverConfigMap.put(server.getName(), server);
                logger.info("加载服务器配置: {}", server.getName());
            }
        } else {
            logger.warn("服务器配置列表为null");
        }
        
        // 初始化监控配置映射
        if (applicationConfig.getMonitorGroup() != null) {
            logger.info("开始加载监控配置，数量: {}", applicationConfig.getMonitorGroup().size());
            for (MonitorConfig monitor : applicationConfig.getMonitorGroup()) {
                monitorConfigMap.put(monitor.getName(), monitor);
                logger.info("加载监控配置: {}, 启用状态: {}, 监控目录: {}", 
                        monitor.getName(), monitor.getEnable(), monitor.getMonitorDir());
            }
        } else {
            logger.warn("监控配置列表为null");
        }
        
        logger.info("配置文件读取器初始化完成，共加载服务器配置: {}, 监控配置: {}", 
                serverConfigMap.size(), monitorConfigMap.size());
    }

    /**
     * 获取所有服务器配置
     */
    public Map<String, ServerConfig> getAllServerConfigs() {
        logger.debug("获取所有服务器配置，当前配置数: {}", serverConfigMap.size());
        return new HashMap<>(serverConfigMap);
    }

    /**
     * 根据名称获取服务器配置
     */
    public ServerConfig getServerConfig(String serverName) {
        logger.debug("获取服务器配置: {}", serverName);
        return serverConfigMap.get(serverName);
    }

    /**
     * 获取所有监控配置
     */
    public Map<String, MonitorConfig> getAllMonitorConfigs() {
        logger.debug("获取所有监控配置，当前配置数: {}", monitorConfigMap.size());
        return new HashMap<>(monitorConfigMap);
    }

    /**
     * 根据名称获取监控配置
     */
    public MonitorConfig getMonitorConfig(String monitorName) {
        logger.debug("获取监控配置: {}", monitorName);
        return monitorConfigMap.get(monitorName);
    }

    /**
     * 获取应用程序主配置
     */
    public ApplicationConfig getApplicationConfig() {
        return applicationConfig;
    }
}